package com.elbis.business.manager;

import java.sql.Date;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.elbis.business.entity.Gamer;
import com.elbis.business.entity.locale.City;
import com.elbis.business.entity.locale.Country;
import com.elbis.business.entity.locale.Language;
import com.elbis.util.HibernateUtil;

public class ManageGamer implements EntityManager<Gamer> {

	private static final Logger logger = Logger.getLogger(ManageGamer.class);

	@Override
	public void delete(Gamer gamer) {
		Session session = HibernateUtil.getSession();
		Transaction trx = null;
		try {
			trx = session.beginTransaction();
			session.delete(gamer);
			trx.commit();
		} catch (HibernateException e) {
			if (trx != null) {
				trx.rollback();
			}
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
	}

	@Override
	public Long save(Gamer gamer) {
		Session session = HibernateUtil.getSession();
		Transaction trx = null;
		Long id = null;
		try {
			trx = session.beginTransaction();
			id = (Long) session.save(gamer);
			trx.commit();
		} catch (HibernateException e) {
			if (trx != null) {
				trx.rollback();
			}
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return id;
	}

	@Override
	public void update(Gamer gamer) {
		Session session = HibernateUtil.getSession();
		Transaction trx = null;
		try {
			trx = session.beginTransaction();
			session.update(gamer);
			trx.commit();
		} catch (HibernateException e) {
			if (trx != null) {
				trx.rollback();
			}
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
	}

	public Gamer getUniqueUser(String email) {
		Session session = HibernateUtil.getSession();
		Gamer gamer = null;
		try {
			gamer = (Gamer) session.createCriteria(Gamer.class)
					.add(Restrictions.and(Restrictions.eq("email", email)))
					.uniqueResult();
		} catch (HibernateException e) {
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return gamer;
	}

	public Gamer getUser(String email, String password) {
		Session session = HibernateUtil.getSession();
		Gamer gamer = null;
		try {
			gamer = (Gamer) session
					.createCriteria(Gamer.class)
					.add(Restrictions.and(Restrictions.eq("email", email),
							Restrictions.eq("password", password)))
					.uniqueResult();
		} catch (HibernateException e) {
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return gamer;
	}

	public Gamer addGamer(String email, String password, String name,
			String surname, Language language, String nationality,
			Country country, City city, Date dateOfBirth) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		Gamer gamer = null;
		try {
			gamer = new Gamer();
			gamer.setEmail(email);
			gamer.setPassword(password);
			gamer.setName(name);
			gamer.setSurname(surname);
			gamer.setNationality(nationality);
			gamer.setDateOfBirth(dateOfBirth);
			tx = session.beginTransaction();
			gamer.setLanguage(language);
			gamer.setCountry(country);
			gamer.setCity(city);
			session.save(gamer);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			logger.error(e.getMessage() + e.getCause());
		} finally {
			session.close();
		}
		return gamer;
	}
}
